﻿using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System.Runtime.InteropServices;

namespace NLBS.Utility
{
    public class AttachmentUnZip
    {
        public AttachmentUnZip()
        {
        }
        public static void UpZip(string zipFile)
        {
            string[] FileProperties = new string[2];
            FileProperties[0] = zipFile;//待解压的文件
            FileProperties[1] = zipFile.Substring(0, zipFile.LastIndexOf("\\") + 1);//解压后放置的目标目录
            UnZipClass UnZc = new UnZipClass();
            UnZc.UnZip(FileProperties);
        }

        public static void UpZip(string zipFile, string savePath)
        {
            string[] FileProperties = new string[2];
            FileProperties[0] = zipFile;//待解压的文件

            FileProperties[1] = savePath;
            UnZipClass UnZc = new UnZipClass();
            UnZc.UnZip(FileProperties);
        }


        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct SHELLEXECUTEINFO
        {
            public int cbSize;
            public uint fMask;
            public IntPtr hwnd;
            public string lpVerb;
            public string lpFile;
            public string lpParameters;
            public string lpDirectory;
            public int nShow;
            public IntPtr hInstApp;
            public int lpIDList;
            public string lpClass;
            public IntPtr hkeyClass;
            public int dwHotKey;
            public IntPtr hIcon;
            public IntPtr hProcess;
        }

        [DllImport("shell32", CharSet = CharSet.Auto)]
        extern static int ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

        /**/
        /// <summary>
        /// 解压压缩包(RAR格式)
        /// </summary>
        /// <param name="directoryName">目录名</param>
        /// <param name="fileName">文件名</param>
        public static void UnRar(string directoryName, string filePath)
        {
            try
            {
                SHELLEXECUTEINFO seInfo = new SHELLEXECUTEINFO();


                seInfo.cbSize = Marshal.SizeOf(seInfo);


                seInfo.lpVerb = "open";

                seInfo.lpFile = System.Configuration.ConfigurationSettings.AppSettings["WINRAR"];

                seInfo.nShow = 5;

                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }

                string param = string.Format(@"E  -ep -inul  -y  -o+  {0}  {1}", filePath, directoryName);

                seInfo.lpParameters = param;

                ShellExecuteEx(ref seInfo);

                System.Threading.Thread.Sleep(4000);

            }
            catch
            {

            }
            finally
            {
                //删除上传的RAR文件
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }


        }
    }
}